<?php

namespace App\Console\Commands;

use App\Events\Message;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class AdvertisingSettlement extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ad:settled';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '广告结算';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        DB::table('ads')
            ->where('status',3)
            ->where('end_time','<=',date('Y-m-d H:i:s'))
            ->orderBy('id')
            ->chunk(30,function ($ads){
                foreach ($ads as $ad){
                    DB::table('ads')
                        ->where('id',$ad->id)
                        ->update(['status' => 4,'finish_time' => date('Y-m-d H:i:s')]);

                    if ($ad->order_user_id && $ad->order_user_amount>0){
                        DB::table('users')
                            ->where('id',$ad->order_user_id)
                            ->increment('balance',$ad->order_user_amount);
                        DB::table('user_balance_details')
                            ->insert([
                                'user_id' => $ad->order_user_id,
                                'change_type' => 1,
                                'type' => 2,
                                'remark' => '广告接单收益',
                                'change_time' => date('Y-m-d H:i:s'),
                                'source_income_user_id' => $ad->user_id,
                            ]);

                        event(new Message([
                            'user_id' =>  $ad->order_user_id,
                            'type' => 4,
                            'content' => '广告接单收益余额收益已入账'
                        ]));
                    }


                    if ($ad->parent_id && $ad->direct_distribution_income>0){
                        DB::table('users')
                            ->where('id',$ad->parent_id)
                            ->increment('balance',$ad->direct_distribution_income);
                        DB::table('user_balance_details')
                            ->insert([
                                'type' => 4,
                                'source_income_user_id' => $ad->user_id,
                                'user_id' => $ad->parent_id,
                                'change_type' => 1,
                                'amount' => $ad->direct_distribution_income,
                                'remark' => '推广收益',
                                'change_time' => date('Y-m-d H:i:s')
                            ]);
                        event(new Message([
                            'user_id' =>  $ad->parent_id,
                            'type' => 4,
                            'content' => '广告推广余额收益已入账'
                        ]));
                    }


                    if ($ad->grand_id && $ad->direct_distribution_income>0){
                        DB::table('users')
                            ->where('id',$ad->grand_id)
                            ->increment('balance',$ad->interposition_income);
                        DB::table('user_balance_details')
                            ->insert([
                                'type' =>4,
                                'source_income_user_id' => $ad->parent_id,
                                'user_id' => $ad->grand_id,
                                'change_type' => 1,
                                'amount' => $ad->interposition_income,
                                'remark' => '推广收益',
                                'change_time' => date('Y-m-d H:i:s')
                            ]);

                        event(new Message([
                            'user_id' =>  $ad->grand_id,
                            'type' => 4,
                            'content' => '广告推广余额收益已入账'
                        ]));
                    }

                }
            });

        return Command::SUCCESS;
    }
}
